import 'package:flutter/material.dart';
import 'package:forui/forui.dart';
import 'package:get/get.dart';
import 'package:home_box/pages/medicine/MedicineVM/medicine_add_vm.dart';

import '../../../../constants.dart';
///药品用法
class DailyFrequency extends StatelessWidget {
  const DailyFrequency({super.key});

  @override
  Widget build(BuildContext context) {
    return GetBuilder<MedicineVM>(
      tag: 'MedicineVM',
      builder: (controller) {
        return Container(
          margin: EdgeInsets.only(top: 10),
          child: GestureDetector(
            onTap: () {
              _showDailyFrequency(context, controller);
            },
            child: Row(
              children: [
                Text('每天次数：'),
                Text(controller.medicine.value.dailyFrequency ?? '选择次数',style: TextStyle(fontSize: 14,color: Colors.grey)),
              ],
            ),
          ),
        );
      },
    );
  }
}

///每天用法弹窗
Future<int?> _showDailyFrequency(BuildContext context, MedicineVM vm) {
  FPickerController controller = FPickerController(
    initialIndexes: [vm.dailyFrequencyIndex.value, 0],
  );

  return showModalBottomSheet(
    context: context,
    builder: (BuildContext context) {
      return SizedBox(
        width: MediaQuery.of(context).size.width,
        child: Column(
          children: [
            SizedBox(
              height: 60,
              child: Stack(
                alignment: Alignment.center,
                children: [
                  Positioned(
                    left: 15,
                    child: SizedBox(
                      width: 15,
                      height: 15,
                      child: GestureDetector(
                        onTap: () {
                          Navigator.pop(context);
                        },
                        child: Image.asset('images/close.png'),
                      ),
                    ),
                  ),
                  Positioned(
                    right: 10,
                    child: GestureDetector(
                      onTap: () {
                        Navigator.pop(context);
                        int index =
                            controller.value[0] %
                            (Constants.dailyFrequency.length);
                        vm.dailyFrequencyIndex.value = index;
                        vm.updateDailyFrequency(
                          Constants.dailyFrequency[index].toString(),
                        );
                      },
                      child: Container(
                        margin: EdgeInsets.all(20.0),
                        child: Text('确认', style: TextStyle(color: Colors.blue)),
                      ),
                    ),
                  ),
                ],
              ),
            ),
            Center(
              child: SizedBox(
                width: 60,
                height: 300,
                child: FPicker(
                  controller: controller,
                  children: [
                    FPickerWheel(
                      flex: 2,
                      loop: true,
                      children:
                          Constants.dailyFrequency.map((value) {
                            return Text('$value');
                          }).toList(),
                    ),
                    FPickerWheel(flex: 1, children: [Text('次')]),
                  ],
                ),
              ),
            ),
          ],
        ),
      );
    },
  );
}
